package campus.service.impl;

import campus.mapper.CashFlowMapper;
import campus.mapper.SubjectsMapper;
import campus.model.CashFlow;
import campus.model.Subjects;
import campus.model.SubjectsExample;
import campus.service.SubjectsService;
import campus.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author VULCAN
 * @create 2020-05-08 20:14
 * @desc 科目实现
 **/
@Service
public class SubjetsServiceImpl implements SubjectsService {

    @Autowired
    private SubjectsMapper subjectsMapper;

    @Autowired
    private CashFlowMapper cashFlowMapper;
    @Override
    public List<Subjects> selectByExample(SubjectsExample example) {
        return subjectsMapper.selectByExample(null);
    }

    @Override
    public Subjects selectByPrimaryKey(Integer kmId) {
        return subjectsMapper.selectByPrimaryKey(kmId);
    }

    @Override
    public int insert(Subjects record) {
        record.setQcYearsNumber(0);
        record.setQcYearsBalance(0);
        return subjectsMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Subjects record) {
        return subjectsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyBeginning(Subjects record) {
        Subjects subjects = subjectsMapper.selectByPrimaryKey(record.getKmId());
        if(record.getQcBeginningBalance() != null ){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcBeginningBalance());
            int i = subjectsMapper.updateByPrimaryKeySelective(record);
            updateXjll();
            return i;
        }else if(record.getQcBorrowerBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() - record.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcLenderBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcLenderBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else{
            record.setQcYearsBalance(subjects.getQcYearsBalance() + subjects.getQcBeginningBalance()+ subjects.getQcLenderBalance() - subjects.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public int updateByPrimaryKeyNumber(Subjects record) {
        Subjects subjects = subjectsMapper.selectByPrimaryKey(record.getKmId());
        if(record.getQcBeginningNumber() != null){
            record.setQcYearsNumber(subjects.getQcYearsNumber() + record.getQcBeginningNumber());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcBorrowerNumber() != null){
            record.setQcYearsNumber(subjects.getQcYearsNumber() - record.getQcBorrowerNumber());
            return  subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcLenderNumber() != null){
            record.setQcYearsNumber(subjects.getQcYearsNumber() + record.getQcLenderNumber());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else {
            record.setQcYearsNumber(subjects.getQcYearsNumber() + subjects.getQcBeginningNumber()+ subjects.getQcLenderNumber() - subjects.getQcBorrowerNumber());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public int updateByPrimaryKeyCost(Subjects record) {
        Subjects subjects = subjectsMapper.selectByPrimaryKey(record.getKmId());
        if(record.getQcBeginningBalance() != null ){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcBeginningBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcBorrowerBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() - record.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcLenderBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcLenderBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else{
            record.setQcYearsBalance(subjects.getQcYearsBalance() + subjects.getQcBeginningBalance()+ subjects.getQcLenderBalance() - subjects.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public int updateByPrimaryKeyLiabilities(Subjects record) {
        Subjects subjects = subjectsMapper.selectByPrimaryKey(record.getKmId());
        if(record.getQcBeginningBalance() != null ){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcBeginningBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcBorrowerBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() + record.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else if(record.getQcLenderBalance() != null){
            record.setQcYearsBalance(subjects.getQcYearsBalance() - record.getQcLenderBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }else{
            record.setQcYearsBalance(subjects.getQcYearsBalance() + subjects.getQcBeginningBalance() - subjects.getQcLenderBalance() + subjects.getQcBorrowerBalance());
            return subjectsMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public int deleteByPrimaryKey(Integer kmId) {
        return subjectsMapper.deleteByPrimaryKey(kmId);
    }

    @Override
    public PageResult findByAllPageSize(Subjects record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Subjects> findlog = (Page<Subjects>) subjectsMapper.findByAllPageSize(record);
        return new PageResult(findlog.getTotal(),findlog.getResult());
    }

    @Override
    public List<Subjects> findByCoding1(String kmCoding) {
        return subjectsMapper.findByCoding(kmCoding);
    }

    @Override
    public List<Subjects> findByCoding2(String kmCoding) {
        return subjectsMapper.findByCoding(kmCoding);
    }

    @Override
    public List<Subjects> findByCoding3(String kmCoding) {
        return subjectsMapper.findByCoding(kmCoding);
    }

    @Override
    public List<Subjects> findByCoding4(String kmCoding) {
        return subjectsMapper.findByCoding(kmCoding);
    }

    @Override
    public List<Subjects> findByCoding5(String kmCoding) {
        return subjectsMapper.findByCoding(kmCoding);
    }
    @Override
    public void updateXjll(){
        Subjects subjects = subjectsMapper.selectSum();
        CashFlow cashFlow = new CashFlow();
        cashFlow.setXmPrice(new BigDecimal(subjects.getQcBeginningBalance()));
        cashFlow.setXmAmount(subjects.getQcBeginningBalance()+"");
        cashFlowMapper.updatexjll1(cashFlow);
        cashFlowMapper.updatexjll2(cashFlow);
    }
}
